<?php

class PropertyReviewsController extends AppController {

    var $name = 'PropertyReviews';

    function index($property_id) {
        $this->layout = "ajax";

        $this->PropertyReview->Property->recursive = -1;
        $this->set('codigo', $this->PropertyReview->Property->find("first", array("conditions" => array("Property.id" => $property_id), "fields" => array("Property.codigo", "Property.id"))));

        if ($property_id == "") {
            $this->Session->setFlash('No ha seleccionado Proyecto');
            $this->redirect(array('controller' => 'Proyects', 'action' => 'index'));
        } else {
            $this->set('revisiones', $this->PropertyReview->find('all', array('conditions' => array('PropertyReview.property_id' => $property_id), 'order' => array('PropertyReview.id DESC'), 'fields' => array('PropertyReview.concepto', 'PropertyReview.calificacion', 'PropertyReview.id', 'PropertyReview.fecha', 'User.nombre', 'User.primer_apellido', 'User.segundo_apellido'))));
        }
    }

    function add($property_id) {
        $cod = $this->Session->read('cod');
        $this->set('cod', $cod);
        $this->set('property_id', $property_id);
        $city = $this->PropertyReview->Property->field('city_id', array('id' => $property_id));
        $dp = $this->PropertyReview->Property->City->field('departament_id', array('id' => $city));
        $this->set('departaments', $this->PropertyReview->Property->City->Departament->find('list', array('order' => 'name ASC')));
        $this->set('cities', $this->PropertyReview->Property->City->find('list', array('order' => 'name ASC', 'conditions' => array('City.departament_id' => $dp))));
        $this->set('dp', $dp);
        $this->PropertyReview->Property->recursive = -1;
        $this->set('codigo', $this->PropertyReview->Property->find("first", array("conditions" => array("Property.id" => $property_id), "fields" => array("Property.codigo"))));

        App::Import('model', 'TitleStudy');
        $estudiosDeTitulo = new TitleStudy();
        $estudiosDeTitulo->recursive = -1;
        //busco el ultimo estudio de titulo del predio
        //tomo el id del estudio de titulo con el busco el RequirementsTitleStudy que me dira si tiene o no venta parcial.
        $ultimoEstudioDeTitulo = $estudiosDeTitulo->find('first', array('conditions' => array('TitleStudy.property_id' => $property_id), 'order' => array('TitleStudy.id DESC'), 'fields' => array('TitleStudy.id')));
        $idUltimoEstudioDeTitulo = $ultimoEstudioDeTitulo['TitleStudy']['id'];


        $this->set('ventaParcial', $estudiosDeTitulo->RequirementsTitleStudy->find('first', array('conditions' => array('RequirementsTitleStudy.requirement_id' => 37, 'RequirementsTitleStudy.title_study_id' => $idUltimoEstudioDeTitulo), 'fields' => array('RequirementsTitleStudy.concepto'))));
        if (!empty($this->data)) {
            $this->data['PropertyReview']['user_id'] = $this->Auth->user('id');
            date_default_timezone_set("America/Bogota");
            $this->data['PropertyReview']['fecha'] = date("Y-m-d-h-i-s");
            if ($this->PropertyReview->saveAll($this->data)) {
                $log = $this->PropertyReview->Property->findLog(array('conditions' => array('Log.action' => 'edit', 'Log.user_id' => $this->Auth->user('id')), 'model' => 'Property', 'fields' => array('change'), 'order' => 'Log.id DESC ', 'limit' => 1));
                $txt = $log[0]['Log']['change'];
                $this->PropertyReview->UpdateAll(array('PropertyReview.cambio' => "'$txt'"), array('PropertyReview.id' => $this->PropertyReview->getLastInsertID()));
                $this->Session->setFlash('Registro guardado correctamente');

                $this->Session->setFlash('Los datos del predio se han guardado con éxito');
                $this->redirect(array('controller' => 'PropertyReviews', 'action' => 'index', $property_id));
            } else {
                $this->Session->setFlash('Error guardando datos');
            }
        } else {
            $this->PropertyReview->Property->recursive = -1;
            $this->data = $this->PropertyReview->Property->find("first", array("conditions" => array("Property.id" => $property_id), 'fields' => array('Property.id', 'Property.nombre', 'Property.city_id', 'Property.proyect_id', 'Property.vereda', 'Property.area_negociacion', 'Property.area_titulo', 'Property.matricula', 'Property.nro_catastral', 'Property.codigo', 'Property.valor_negociacion')));
        }
    }

}

?>
